﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace CorporationWars
{
    class UIPopupMessageBox
    {
        public Game1 Game;
        public TimeSpan msgBoxPopupDuration = new TimeSpan(0, 0, 0, 5, 0);
        TimeSpan msgBoxShowStart;

        public string PopUpTitle;
        public  string PopUpMessage;
        int popupgWidth;
        int popupHeight;
        Color popupColor;
        float popAlpha = 1;
        float popupFadeSpeed = .05f;
        Color popupTitleBGColor;
        Color popupTitleColor;

        public Vector2 PositionOffset { get; set; }

        public bool FadedOut { get { return popAlpha == 0; } }

        public UIPopupMessageBox(Game1 game, string title, string message, int width, int height, Color color, Color titleGBColor, Color titleColor, Vector2 positionOffset, float fadeSpeed = .05f) 
            : this(game,title,message,width,height,color,titleGBColor,titleColor,fadeSpeed)
        {
            PositionOffset = positionOffset;
        }

        public UIPopupMessageBox(Game1 game, string title, string message, int width, int height, Color color, Color titleGBColor, Color titleColor, float fadeSpeed = .05f)
        {
            Game = game;
            popAlpha = 1;
            msgBoxShowStart = DateTime.Now.TimeOfDay;
            PopUpTitle = title;
            PopUpMessage = message;
            popupColor = color;
            popupgWidth = width;
            popupHeight = height;
            popupFadeSpeed = fadeSpeed;
            popupTitleBGColor = titleGBColor;
            popupTitleColor = titleColor;
        }

        public void Draw(GameTime gameTime)
        {
            // Manage the fade..
            if (msgBoxPopupDuration - (DateTime.Now.TimeOfDay - msgBoxShowStart) < TimeSpan.Zero)
                popAlpha -= popupFadeSpeed;

            if (popAlpha <= 0)
                popAlpha = 0;

            Rectangle boxRect = new Rectangle((int)PositionOffset.X + (Game.GraphicsDevice.Viewport.Width / 2) - (popupgWidth / 2), (int)PositionOffset.Y + (Game.GraphicsDevice.Viewport.Height / 2) - (popupHeight / 2), popupgWidth, popupHeight);
            Rectangle titleRect = new Rectangle(boxRect.Left + 4, boxRect.Top + 4, boxRect.Width - 8, (int)Game.Content.Load<SpriteFont>("Fonts/cyberpunkFont").MeasureString(PopUpTitle).Y + 8);

            popupColor.A = (byte)(popAlpha * 255);
            popupTitleBGColor.A = popupColor.A;
            popupTitleColor.A = popupColor.A;

            SpriteBatch spriteBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));

            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied);
            // BG
            spriteBatch.Draw(Game.Content.Load<Texture2D>("Textures/halfShade"), boxRect, popupColor);
            // TitleBar
            spriteBatch.Draw(Game.Content.Load<Texture2D>("Textures/halfwhiteToNothing"), titleRect, popupTitleBGColor);

            // Title
            Game.RenderText(PopUpTitle, new Vector2(titleRect.Left + 8, titleRect.Top + 8), popupTitleColor, -1, "Fonts/cyberpunkFont");

            // Text 
            Game.RenderText(PopUpMessage, new Vector2(boxRect.Left + 8, titleRect.Bottom + 8), popupColor, boxRect.Width - 16, "Fonts/cyberpunkFont");

            spriteBatch.End();
        }
    }
}
